﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MedicalDB;
using ZedGraph;
using NHibernateDB;

namespace MedicalDB
{
    public partial class GraficBP : Form
    {

        DateTime dataInceput;
        DateTime dataSfarsit;
        int idsenzor;
        string CNP = string.Empty;
       
        ZedGraphControl zgc1;
        ZedGraphControl zgc2;
        int idImagine = 0;
        GraphPane myPane1;
        GraphPane myPane2;
        List<string> namesForm;

        public GraficBP(DateTime dataInceput, DateTime dataSfarsit, int idsenzor, string CNP,string limba)
        {
            InitializeComponent();
            this.dataInceput = dataInceput;
            this.dataSfarsit = dataSfarsit;
            this.idsenzor = idsenzor;
            this.CNP = CNP;
            this.zgc1 = zg1;
            this.zgc2 = zg2;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            namesForm = xmlParser.ExtractAttribute("GraficBP", string.Empty, string.Empty, "name");                       
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateGraph();
            CreateGraph2();
            SetSize();
        }

        public void CreateGraph()
        {
            GraphPane myPane = zgc1.GraphPane;
            this.myPane1 = myPane;

            // Set the titles and axis labels

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> listdate = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", CNP);

            myPane.Title.Text = namesForm[0] + listdate[0].Nume + " " + listdate[0].Prenume;
            myPane.XAxis.Title.Text = namesForm[2] + dataInceput.ToString() + namesForm[3] + dataSfarsit.ToString();
            myPane.YAxis.Title.Text = namesForm[4];

            PointPairList list = new PointPairList();
            PointPairList list2 = new PointPairList();

            IList<SenzoriBP> bpList = pm.RetrieveEquals<SenzoriBP>("IdPacient", CNP, "IdSenzor", idsenzor);

            DateTime dataAnterioara = DateTime.Today;
            Boolean first = true;
            int diff = 0;
            foreach (SenzoriBP bp in bpList)
            {
                if ((bp.Data.CompareTo(dataInceput) >= 0) && bp.Data.CompareTo(dataSfarsit) <= 0)
                {

                    if (first)
                    {
                        list.Add(0, bp.Sys);
                        first = false;
                        dataAnterioara = bp.Data;
                    }
                    else
                    {
                        TimeSpan ts = bp.Data - dataAnterioara;
                        // Difference in days.
                        int differeneInseconds = ts.Seconds;
                        diff += differeneInseconds;
                        list.Add(diff, bp.Sys);
                        dataAnterioara = bp.Data;
                    }

                }
            }


            // Generate a blue curve with circle symbols, and "My Curve 2" in the legend
            LineItem myCurve = myPane.AddCurve(namesForm[0], list, Color.Blue,
                                   SymbolType.Circle);
            // Fill the area under the curve with a white-red gradient at 45 degrees
            myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);

            // Make the symbols opaque by filling them with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            zgc1.AxisChange();
        }

        public List<string> SaveImage()
        {
            Bitmap bmp = myPane1.GetImage();
            string filename1 = "C:\\BP1-" + (idImagine++) + ".jpg";
            bmp.Save(filename1, System.Drawing.Imaging.ImageFormat.Jpeg);
            Bitmap bmp2 = myPane1.GetImage();
            string filename2 = "C:\\BP2-" + (idImagine++) + ".jpg";
            bmp2.Save(filename2, System.Drawing.Imaging.ImageFormat.Jpeg);
            return new List<string> { filename1, filename2 };
        }

        public void CreateGraph2()
        {
            GraphPane myPane = zgc2.GraphPane;
            this.myPane2 = myPane;

            // Set the titles and axis labels

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> listdate = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", CNP);

            myPane.Title.Text = namesForm[1] + listdate[0].Nume + " " + listdate[0].Prenume;
            myPane.XAxis.Title.Text = namesForm[2] + dataInceput.ToString() + namesForm[3] + dataSfarsit.ToString();
            myPane.YAxis.Title.Text = namesForm[5];

            PointPairList list = new PointPairList();
            
            IList<SenzoriBP> bpList = pm.RetrieveEquals<SenzoriBP>("IdPacient", CNP, "IdSenzor", idsenzor);

            DateTime dataAnterioara = DateTime.Today;
            Boolean first = true;
            int diff = 0;
            foreach (SenzoriBP bp in bpList)
            {
                if ((bp.Data.CompareTo(dataInceput) >= 0) && bp.Data.CompareTo(dataSfarsit) <= 0)
                {

                    if (first)
                    {
                        list.Add(0, bp.Dia);
                        first = false;
                        dataAnterioara = bp.Data;
                    }
                    else
                    {
                        TimeSpan ts = bp.Data - dataAnterioara;
                        // Difference in days.
                        int differeneInseconds = ts.Seconds;
                        diff += differeneInseconds;
                        list.Add(diff, bp.Dia);
                        dataAnterioara = bp.Data;
                    }

                }
            }


            // Generate a blue curve with circle symbols, and "My Curve 2" in the legend
            LineItem myCurve = myPane.AddCurve(namesForm[1], list, Color.Blue,
                                   SymbolType.Circle);
            // Fill the area under the curve with a white-red gradient at 45 degrees
            myCurve.Line.Fill = new Fill(Color.White, Color.Red, 45F);

            // Make the symbols opaque by filling them with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45F);

            // Fill the pane background with a color gradient
            myPane.Fill = new Fill(Color.White, Color.FromArgb(220, 220, 255), 45F);

            // Calculate the Axis Scale Ranges
            zgc2.AxisChange();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            SetSize();
        }

        private void SetSize()
        {
            zg1.Location = new Point(10, 10);
            // Leave a small margin around the outside of the control
            zg1.Size = new Size(this.ClientRectangle.Width / 2 - 20, this.ClientRectangle.Height - 20);
            zg2.Location = new Point(this.ClientRectangle.Width / 2 + 10, 10);
            zg2.Size = new Size(this.ClientRectangle.Width / 2 - 20, this.ClientRectangle.Height - 20);

        }
    }
}